package com.wangjiyuan.giftshuo.ui.fragment.classify.strategy.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.bean.classifybean.strategy.ColumnBean;
import com.wangjiyuan.giftshuo.ui.activity.classify.strategy_details.column_details.all_details.ColumnDetailsActivity;
import com.wangjiyuan.giftshuo.ui.activity.other.more.MoreActivity;
import com.wangjiyuan.giftshuo.widget.coustomview.RectangleBac;
import com.wangjiyuan.giftshuo.utils.RecyclerViewAdapterHelper;

import java.util.List;

import static com.wangjiyuan.giftshuo.config.classify.ConstantLength.IMAGEVIEW_PADDING_HEIGH;
import static com.wangjiyuan.giftshuo.config.classify.ConstantLength.IMAGEVIEW_PADDING_WIDTH;

/**
 * Created by QXL on 2016/11/17.
 * 栏目的RecyclerView
 */

public class ColumnRecyclerAdapter extends RecyclerViewAdapterHelper<Object> {

    public static final int TYPE1 = 0, TYPE2 = 1;

    public ColumnRecyclerAdapter(Context context, List<Object> list) {
        super(context, list);
    }

    @Override
    public int getItemViewType(int position) {
        if (mList.get(position) instanceof ColumnBean.DataBean.PagingBean) {
            return TYPE2;
        } else {
            return TYPE1;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE1:
                view = mInflater.inflate(R.layout.itemcolumn1, parent, false);
                return new ViewHolderColumn1(view);
            case TYPE2:
                view = mInflater.inflate(R.layout.itemcolumn2, parent, false);
                return new ViewHolderColumn2(view);
        }
        return null;
    }

    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, final int position) {
        int widthPixels = mContext.getResources().getDisplayMetrics().widthPixels;
        int heightPixels = mContext.getResources().getDisplayMetrics().heightPixels;
        if (holder instanceof ViewHolderColumn1) {
            ColumnBean.DataBean dataBean = (ColumnBean.DataBean) mList.get(position);
//            设置view的边距
            ((ViewHolderColumn1) holder).textView_itemcolumn_title.setText(dataBean.getColumns().get(position).getTitle());
            ((ViewHolderColumn1) holder).textView_itemcolumn_title.setHeight(heightPixels / 12);
            ((ViewHolderColumn1) holder).textView_itemcolumn_title.setWidth(widthPixels * 2 / 5);

            ((ViewHolderColumn1) holder).textView_itemcolunm_user.setText(dataBean.getColumns().get(position).getAuthor());
            ((ViewHolderColumn1) holder).textView_itemcolunm_user.setHeight(heightPixels / 12);
            ((ViewHolderColumn1) holder).textView_itemcolunm_user.setWidth(widthPixels * 2 / 5);

            Glide.with(mContext).load(dataBean.getColumns().get(position).getBannerImageUrl())
                    .override(widthPixels * 2 / 5, heightPixels / 3)
                    .into(((ViewHolderColumn1) holder).imageView_itemcolumn);
//            传递被点击的item的ID
            ((ViewHolderColumn1) holder).checkItemID(dataBean.getColumns().get(position).getId());
        } else {
            ((ViewHolderColumn2) holder).textView_rectangleBac.setWidth(widthPixels * 2 / 5);
            ((ViewHolderColumn2) holder).textView_rectangleBac.setHeight(heightPixels / 18);
        }
    }

    class ViewHolderColumn1 extends RecyclerView.ViewHolder implements View.OnClickListener {

        RelativeLayout relativeLayout_itemcolumn;
        ImageView imageView_itemcolumn;
        TextView textView_itemcolumn_title, textView_itemcolunm_user;

        public ViewHolderColumn1(View itemView) {
            super(itemView);
            relativeLayout_itemcolumn = (RelativeLayout) itemView.findViewById(R.id.relativeLayout_itemcolumn);
//            item的点击监听
            relativeLayout_itemcolumn.setOnClickListener(this);

            imageView_itemcolumn = (ImageView) itemView.findViewById(R.id.imageView_itemcolumn);
            imageView_itemcolumn.setPadding(IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH,
                    IMAGEVIEW_PADDING_WIDTH, IMAGEVIEW_PADDING_HEIGH);

            textView_itemcolumn_title = (TextView) itemView.findViewById(R.id.textView_itemcolumn_title);
            textView_itemcolunm_user = (TextView) itemView.findViewById(R.id.textView_itemcolunm_user);
        }

        //        获得栏目中item的ID
        private int itemID;

        public void checkItemID(int ID) {
            this.itemID = ID;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.relativeLayout_itemcolumn:
                    Intent intent = new Intent(mContext, MoreActivity.class);
                    intent.putExtra("id", itemID);
                    mContext.startActivity(intent);
                    break;
            }
        }
    }

    //    点击查看全部布局
    class ViewHolderColumn2 extends RecyclerView.ViewHolder implements View.OnClickListener {
        LinearLayout linearLayout_itemcolumn2;
        RectangleBac textView_rectangleBac;

        public ViewHolderColumn2(View itemView) {
            super(itemView);
            linearLayout_itemcolumn2 = (LinearLayout) itemView.findViewById(R.id.linearLayout_itemcolumn2);

            textView_rectangleBac = (RectangleBac) itemView.findViewById(R.id.textView_rectangleBac);
            textView_rectangleBac.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.textView_rectangleBac:
                    Intent intent = new Intent(mContext, ColumnDetailsActivity.class);
                    mContext.startActivity(intent);
                    break;
            }
        }
    }
}
